<?php
/**
 * 
 * String user_id
 * String user_name【用户名】
 * String user_pass【用户密码】
 * String true_name【真实姓名】
 * String add_time【添加时间】
 * String is_admin【是否管理员:0为否,1为是】
 * String user_type【用户类型：0未分类，1为管理团队，2为运营团队，3为技术团队，4为仓储行政团队】
 * String permission【权限】
 */
class App_Model_User extends App_Model_Abstract
{
	public function init()
	{
		parent::init();
		$this->table = App_Db_Table::factory('User');
	}
	
	public function add($row)
	{
		return $this->table->insert($row);
	}
	
	public function update($row, $id)
	{
		$db = $this->table->getAdapter();
		$where = $db->quoteInto('user_id = ?', $id);
		
		return $this->table->update($row, $where);
	}
	
	public function delete($id)
	{
		$db = $this->table->getAdapter();
		$where = $db->quoteInto('user_id = ?', $id);
		
		return $this->table->delete($where);
	}
	
	public function exist($username)
	{
		return $this->table->fetchRow(array('user_name = ?'=>$username));
	}
	
	public function listAll()
	{
		return $this->table->fetchAll();
	}
	
	public function login($username, $password)
	{
		$row = $this->table->fetchRow(array('user_name = ?'=>$username, 'user_pass = ?'=>md5($password)));
		if ($row) {
			$userNameSpace = new Zend_Session_Namespace('User');
        	$userNameSpace->user = $row;
        	return true;
		}
		return false;
	}
	
	public function getOne($id)
	{
		$row = $this->table->find($id);
		if ($row) {
			return $row->current();
		}
		return array();
	}

	public function getByType($type)
	{
		$db= $this->table->getAdapter();
		$where = $db->quoteInto('user_type in (?)',$type);
		$rows = $this->table->fetchAll($where);
		return $rows;
	}
}